package com.cmol.peopledetectapp.admin;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cmol.peopledetectapp.R;

import java.util.List;

/**
 * RecyclerView 辅助类,系统日志列表
 *
 * @author 张瀚文
 */
public class MyAdapterLog extends RecyclerView.Adapter<MyAdapterLog.MyViewHolder> {

    private List<BeanLog> data;
    private Context context;
    private onRecyclerItemClickListener mOnRecyclerItemClickListener;

    public MyAdapterLog(List<BeanLog> data, Context context) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_log, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.operationText.setText(data.get(position).getOperation());
        holder.nameText.setText(data.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setRecyclerItemClickListener(onRecyclerItemClickListener listener) {
        mOnRecyclerItemClickListener = listener;
    }

    public interface onRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView operationText, nameText;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            operationText = itemView.findViewById(R.id.OperationText);
            nameText = itemView.findViewById(R.id.NameText);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnRecyclerItemClickListener != null) {
                        mOnRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

}